/*
 * ClientGUI.java
 *
 * Created on May 3, 2007, 10:41 AM
 *
 *
 * Copyright (c) 2008 Golden T Studios.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package test.chat;

// JFC
import java.io.IOException;

import com.golden.gamedev.engine.network.NetworkPacket;
import com.golden.gamedev.engine.network.packet.NetworkMessage;
import com.golden.gamedev.engine.network.tcp.TCPClient;

/**
 *
 * @author  Paulus Tuerah
 */
public class ClientGUI extends javax.swing.JFrame implements Runnable {

	private TCPClient	client;
	private String		nick;
	
	
 /****************************************************************************/
 /******************************* CONSTRUCTOR ********************************/
 /****************************************************************************/
	
	/** Creates new form ClientGUI */
	public ClientGUI(TCPClient client, String nick) {
		initComponents();
		
		this.client = client;
		this.nick	= nick;
		
		new Thread(this).start();
		
		setSize(500, 400);
		setLocationRelativeTo(null);
		txtMessage.requestFocus();
	}


	public void run() {
		try {
			while (true) {
				client.update(100);
				
				NetworkPacket[] receivedPackets = client.getReceivedPackets();
				for (int i=0;i < receivedPackets.length;i++) {
					NetworkMessage packet = (NetworkMessage) receivedPackets[i];
					
					appendText(packet.getMessage() + "\n");
				}
				
				try {
					Thread.sleep(100);
					
				} catch (InterruptedException ex) {
					ex.printStackTrace();
				}
			}
			
		} catch (IOException ex) {
			ex.printStackTrace();
			System.exit(-1);
		}
	}
	
	private void appendText(String text) {
		txtChat.setText(txtChat.getText() + text);

		txtChat.setCaretPosition(txtChat.getDocument().getLength());
	}
	
	
	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        javax.swing.JLabel jLabel1;
        javax.swing.JPanel jPanel1;
        javax.swing.JPanel jPanel2;

        jPanel2 = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txtMessage = new javax.swing.JTextField();
        btnSend = new javax.swing.JButton();
        scrollText = new javax.swing.JScrollPane();
        txtChat = new javax.swing.JEditorPane();

        FormListener formListener = new FormListener();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Chat Room");
        jPanel2.setLayout(new java.awt.BorderLayout(5, 5));

        jPanel2.setBorder(javax.swing.BorderFactory.createEmptyBorder(10, 10, 10, 10));
        jPanel1.setLayout(new java.awt.BorderLayout(5, 0));

        jLabel1.setText("Message :");
        jPanel1.add(jLabel1, java.awt.BorderLayout.WEST);

        txtMessage.addActionListener(formListener);

        jPanel1.add(txtMessage, java.awt.BorderLayout.CENTER);

        btnSend.setText("Send");
        btnSend.addActionListener(formListener);

        jPanel1.add(btnSend, java.awt.BorderLayout.EAST);

        jPanel2.add(jPanel1, java.awt.BorderLayout.SOUTH);

        txtChat.setEditable(false);
        scrollText.setViewportView(txtChat);

        jPanel2.add(scrollText, java.awt.BorderLayout.CENTER);

        getContentPane().add(jPanel2, java.awt.BorderLayout.CENTER);

        pack();
    }

    // Code for dispatching events from components to event handlers.

    private class FormListener implements java.awt.event.ActionListener {
        FormListener() {}
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            if (evt.getSource() == txtMessage) {
                ClientGUI.this.btnSendActionPerformed(evt);
            }
            else if (evt.getSource() == btnSend) {
                ClientGUI.this.btnSendActionPerformed(evt);
            }
        }
    }// </editor-fold>//GEN-END:initComponents

	private void btnSendActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSendActionPerformed
		String message = txtMessage.getText().trim();
		if (message.length() == 0) return;
		
		try {
			client.sendPacket(new NetworkMessage(message));

			message = nick + ": " + message;
			
			txtMessage.selectAll();
			
			appendText(message + "\n");
			
		} catch (IOException ex) {
			ex.printStackTrace();
		}
	}//GEN-LAST:event_btnSendActionPerformed

//    public static void main(String args[]) {
//        java.awt.EventQueue.invokeLater(new Runnable() {
//            public void run() {
//                new ClientGUI().setVisible(true);
//            }
//        } );
//    }
	
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnSend;
    private javax.swing.JScrollPane scrollText;
    private javax.swing.JEditorPane txtChat;
    private javax.swing.JTextField txtMessage;
    // End of variables declaration//GEN-END:variables
	
}
